package com.example.ch02_student.adminsDo;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.example.ch02_student.R;
import com.example.ch02_student.entity.RolePremissionVo;
import com.example.ch02_student.utils.ResultVo;
import com.example.ch02_student.utils.UrlBase;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class Shiro_manneage extends AppCompatActivity {
    private ListView listView;
    private OkHttpClient okHttpClient;
    private List<Map<String, String>> list = new ArrayList<>();
    private AlertDialog dialog = null;
    private String[] shiro = new String[]{
            "sTeaInfo", "aTeaInfo", "uTeaInfo", "dTeaInfo",
            "aStuInfo", "sStuInfo", "uStuInfo", "dStuInfo",
            "sCouInfo", "aCouInfo", "sGraInfo", "uGraInfo"};
    boolean[] booleans = new boolean[12];
    private String xzShiro = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shior_manneage);
        okHttpClient = new OkHttpClient();

        listView = findViewById(R.id.lv);
        //初始化
        init();
        //点击触发弹窗事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                final int a = i;
                Toast.makeText(Shiro_manneage.this, "success", Toast.LENGTH_SHORT).show();

                AlertDialog.Builder builder = new AlertDialog.Builder(Shiro_manneage.this)
                        .setTitle("选择赋予权限")
                        .setMultiChoiceItems(shiro, booleans, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                                booleans[i] = b;
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                StringBuilder stringBuilder = new StringBuilder();
                                for (int j = 0; j < booleans.length; j++) {
                                    if (booleans[j]) {
                                        stringBuilder.append("," + shiro[j]);
                                    }
                                }
                                String s = "";
                                if (stringBuilder.indexOf(",") == 0) {
                                    xzShiro = stringBuilder.substring(1);
                                }
                                System.out.println(list.get(a).get("no"));
                                String no = list.get(a).get("no");
                                //发送请求
                                Request build = new Request.Builder().url(UrlBase.URL_BASE + "/admins/updatePer?no=" + no + "&pers=" + xzShiro).build();
                                Call call = okHttpClient.newCall(build);
                                call.enqueue(new Callback() {
                                    @Override
                                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                                    }

                                    @Override
                                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                        String string = response.body().string();
                                        ResultVo resultVo = new Gson().fromJson(string, ResultVo.class);
                                        if (resultVo.getCode() == 1000) {
                                            Looper.prepare();
                                            Toast.makeText(Shiro_manneage.this, xzShiro, Toast.LENGTH_SHORT).show();
                                            Looper.loop();
                                            //回到主线程
                                        }
                                        System.err.println("success");
                                    }
                                });
                               init();

                                //
                                Toast.makeText(Shiro_manneage.this, xzShiro, Toast.LENGTH_SHORT).show();
                                dialog.dismiss();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialog.dismiss();
                            }
                        });
                dialog = builder.create();
                dialog.show();


            }
        });

    }


    public void init() {
        final Request build = new Request.Builder().url(UrlBase.URL_BASE + "/admins/selectAllPer").build();
        Call call = okHttpClient.newCall(build);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                list.removeAll(list);
                String string = response.body().string();
                RolePremissionVo[] rolePremissionVos = new Gson().fromJson(string, RolePremissionVo[].class);
                for (int i = 0; i < rolePremissionVos.length; i++) {
                    Map<String, String> map = new HashMap<>();
                    map.put("no", rolePremissionVos[i].getRole());
                    StringBuilder builder = new StringBuilder();
                    for (int j = 0; j < rolePremissionVos[i].getList().size(); j++) {
                        String s = rolePremissionVos[i].getList().get(j);
                        builder.append(s + ",");
                    }
                    map.put("permission", builder + "");
                    list.add(map);
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        String[] s = new String[]{"no", "permission"};
                        int[] to = new int[]{R.id.no, R.id.permission};
                        SimpleAdapter simpleAdapter = new SimpleAdapter(Shiro_manneage.this, list, R.layout.activity_shior_manneage_items, s, to);
                        listView.setAdapter(simpleAdapter);
                    }
                });
            }
        });
    }
}
